/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.grnt.common.ContextWraper;
import org.grnt.common.TwoTuple;

/**
 *
 * This is a Servlet class for init some context's content. And it must be auto
 * instanced when service startup and after ContextWraper inited.
 * @author sayid
 */
public class BuddiesContext {
	/**
	 *
	 * Hold users into a map, and the index is user id.
	 */
	public static class Users extends TreeMap<Long, UserDecorator>{}
	/**
	 *
	 * Hold buddies into a map, and the index is buddy id.
	 */
	public static class Buddies extends TreeMap<Long, BuddyDecorator>{}
	/**
	 *
	 * It is hash map index, the index field is user's login name and value is user id.
	 */
	public static class UsersIndex extends HashMap<String, Long>{}
	/**
	 *
	 * It is hash map index, the index field is buddy's alias name and value is buddy id.
	 */
	public static class BuddiesIndex extends HashMap<String, Long>{}
	/**
	 *
	 * It holds users' read-write-locks.
	 */
	public static class UserLocks extends TreeMap<Long, WeakReference<TwoTuple<ReadLock, WriteLock>>>{}

	/**
	 *
	 * @return A map in context, which holds users.
	 */
	public static final Users getUsers(){
		return (Users)ContextWraper.getAttribute("users");
	}

	/**
	 *
	 * @return A map in context, which holds buddies.
	 */
	public static final Buddies getBuddies() {
		return (Buddies) ContextWraper.getAttribute("buddies");
	}

	/**
	 *
	 * @return A map in context, which holds user's login name index.
	 */
	public static final UsersIndex getUsersIndex() {
		return (UsersIndex) ContextWraper.getAttribute("usersIndex");
	}

	/**
	 *
	 * @return A map in context, which holds buddy's alias index.
	 */
	public static final BuddiesIndex getBuddiesIndex(){
		return (BuddiesIndex) ContextWraper.getAttribute("buddiesIndex");
	}

	/**
	 *
	 * @return A map incontext, which holds a list read-write-lock, and every one
	 * reference to a correct user.
	 */
	public static final UserLocks getUserLocks() {
		return (UserLocks) ContextWraper.getAttribute("userLocks");
	}
}
